package com.edu.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.api.entity.comment.CourseComment;
import com.edu.api.entity.comment.CourseCommentFavoriteRecord;
import com.edu.api.entity.commentdto.CourseCommentDto;
import com.edu.api.entity.common.*;
import com.edu.comment.mapper.CourseCommentFavoriteRecordMapper;
import com.edu.comment.service.CourseCommentService;
import com.edu.comment.mapper.CourseCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@Service
public class CourseCommentServiceImpl extends ServiceImpl<CourseCommentMapper, CourseComment>
    implements CourseCommentService{

    @Autowired
    private CourseCommentMapper courseCommentMapper;

    @Autowired
    private CourseCommentService courseCommentService;

    @Autowired
    private CourseCommentFavoriteRecordMapper recordMapper;

    @Override
    public ResponseDTO saveOrCancelFavorite(Integer commentId, HttpServletRequest request) {
        //查询点赞记录
        QueryWrapper<CourseCommentFavoriteRecord> rq = new QueryWrapper<>();
        Integer userId = Integer.valueOf(JwtUtil.getMemberIdByJwtToken(request));
        rq.eq("comment_id",commentId).eq("user_id", userId);
        CourseCommentFavoriteRecord selectRecord = recordMapper.selectOne(rq);
        //修改点赞记录
        UpdateWrapper<CourseComment> uw = new UpdateWrapper<>();
        //第一次点赞
        if (selectRecord == null){
            CourseCommentFavoriteRecord record = new CourseCommentFavoriteRecord();
            record.setUserId(userId);
            record.setCommentId(commentId);
            record.setIsDel(false);
            record.setCreateTime(TimeUtil.getTimeDate());
            record.setUpdateTime(TimeUtil.getTimeDate());
            recordMapper.insert(record);
            uw.eq("id",commentId);
            uw.setSql("like_count = like_count + 1");
            courseCommentService.update(uw);
            return new ResponseDTO(Code.GET_OK,"成功",null);
        }

        courseCommentMapper.updateCommentAndFavoriteRecord(selectRecord.getId(),selectRecord.getIsDel());

        return new ResponseDTO(Code.GET_OK,"成功",null);
    }

    @Override
    public ResponseDTO<Page<CourseCommentDto>> getCourseCommentList(String courseId, Pageable pageable) {
        QueryWrapper<CourseComment> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("course_id",courseId).eq("section_id",0).eq("lesson_id",0).eq("is_del",0);
        long total = courseCommentMapper.selectCount(objectQueryWrapper);
        long pages = 0;
        if(total>0){
            //计算总页码
            pages = total % pageable.getSize() == 0 ? total / pageable.getSize() : total / pageable.getSize() + 1;
            //页码修正
            pageable.setPage(pageable.getPage() < 1 ? 1 : pageable.getPage());
            pageable.setPage(pageable.getPage() > pages ? pages : pageable.getPage());
            //起始下标
            pageable.setIndex((pageable.getPage() - 1) * pageable.getSize());
        }else{
            pageable.setPage(0);
            pageable.setIndex(0);
        }
        Page<CourseCommentDto> courseCommentDtoPage = new Page<>(courseCommentMapper.getCourseCommentList(courseId, pageable), pageable.getPage(), total, pages);
        return new ResponseDTO<Page<CourseCommentDto>>(Code.GET_OK,"成功",courseCommentDtoPage);
    }

    @Override
    public ResponseDTO<Integer> saveCourseComment(String courseId, String userId, String userName, String comment) {
        return new ResponseDTO<Integer>(Code.GET_OK,"成功",courseCommentMapper.saveCourseComment(courseId,userId,userName,comment));
    }
}




